package com.xiao.controller;

import com.github.pagehelper.PageInfo;

import com.xiao.pojo.Student;


import com.xiao.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;


    @RequestMapping(value = "studentLogin", method = RequestMethod.POST)
    public ModelAndView login(@RequestParam("sno") String sno, @RequestParam("spassword") String spassword, HttpServletRequest request, HttpServletResponse response, Model model) throws ServletException, IOException {
        ModelAndView mv = new ModelAndView();
        Student student = studentService.selectStudentBySNo(sno);
        HttpSession session = request.getSession();
        session.setAttribute("stu", student);
//        response.sendRedirect(request.getContextPath()+"stu_page.jsp");
        if (student != null) {
            if (student.getSpassword().equals(spassword)) {
                System.out.println("登录成功");
                session.setAttribute("user", student);
//response.sendRedirect(request.getContextPath()+"/jsp/stu_page.jsp");
                mv.setViewName("stu_page");
                return mv;

            } else {
//request.setAttribute("login_msg","用户名或密码错误");
//request.getRequestDispatcher("/jsp/index.jsp").forward(request,response);
//response.sendRedirect("/jsp/index.jsp");
                model.addAttribute("login_msg", "用户名或密码错误");
                mv.setViewName("index");
            }
        } else {
//            request.setAttribute("login_msg","账号不存在");
//            request.getRequestDispatcher("/jsp/index.jsp").forward(request,response);
            model.addAttribute("login_msg", "账号不存在");
            mv.setViewName("index");
            return mv;
        }
        return mv;
    }

    /* //跳转到增加学生页面
     @RequestMapping("/toAddStudent")
     public String toAddStudentPaper(){
         return "addStudent";
     }*/
    //判断用户名是否存在
    @RequestMapping(value = "registerCheck", method = RequestMethod.GET)
    public void login(@RequestParam("sno") String sno, HttpServletRequest request, HttpServletResponse response, Model model) throws ServletException, IOException {
        Student student = studentService.selectStudentBySNo(sno);
        if (student != null) {
            response.getWriter().print("exit");
        }

    }

    /*注册请求*/
    @RequestMapping("/addStudent")
    public String addStudent(Student student, HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("addBook=>" + student);
        String sname = student.getSname();
        studentService.addStudent(student);
        request.setAttribute("msg", sname);
        return "loginSuccess";
    }

    //学生列表分页查询
    @RequestMapping("/student-list")
    public ModelAndView admin_findAll(@RequestParam(name = "page", required = true, defaultValue = "1") int page, @RequestParam(name = "size", required = true, defaultValue = "4") int size) throws Exception {
        ModelAndView mv = new ModelAndView();
        List<Student> list = studentService.queryAllStudent(page, size);
        PageInfo pageInfo = new PageInfo(list);

        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("stu_list");
        return mv;
    }

    //跳转到修改页面
    @RequestMapping("/toUpdate")
    public String toUpdatePaper(String sno, Model model) {
        Student student = studentService.selectStudentBySNo(sno);
        model.addAttribute("Qstu", student);
        return "updateStudent";
    }
  //删除学生
    @RequestMapping("/deleteStu/{sid}")
    public String deleteStu(@PathVariable("sid") int id) {
        studentService.deleteStu(id);
        return "redirect:/student/student-list";
    }
  //查询学生
    @RequestMapping("/queryStu")
    public ModelAndView queryStu(@RequestParam(name = "page", required = true, defaultValue = "1") int page, @RequestParam(name = "size", required = true, defaultValue = "4") int size, String sno, Model model) {
        ModelAndView mv = new ModelAndView();
        Student student = studentService.selectStudentBySNo(sno);
        List<Student> studentList = new ArrayList<Student>();
        studentList.add(student);
        if (student == null) {
            List<Student> list = studentService.queryAllStudent(page, size);
            studentList = list;
            PageInfo pageInfo = new PageInfo(studentList);
            mv.addObject("pageInfo", pageInfo);
            model.addAttribute("error", "未查到");
            mv.setViewName("stu_list");
            return mv;
        } else {
            PageInfo pageInfo = new PageInfo(studentList);
            mv.addObject("pageInfo", pageInfo);
        }
 mv.setViewName("StuquerySuccess");
        return mv;
    }

}